package com.pkk.alert.common.feige;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pkk.alert.common.dto.FeiGeReqDataVo;
import com.pkk.alert.common.vo.feige.FeigeUserRespVo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: 构建飞鸽的请求信息类
 * @author: peikunkun
 * @create: 2019-07-02 17:54
 **/
@Slf4j
public class FeigeComonBuild {

  /**
   * 获取飞鸽用户信息
   */
  private static final String FEIGE_GET_USER_URL = "http://jy.erpit.cn/api/userlist";

  /**
   * 飞鸽给单用户发送信息-新版
   */
  public static final String FEIGE_SEND_USER_SINGLE_NEW = "http://jy.erpit.cn/api/message/send-user";

  /**
   * 飞鸽给多用户(群)发送信息-新版
   */
  public static final String FEIGE_SEND_USER_MORE_NEW = "http://jy.erpit.cn/api/message/send";


  /**
   * @Description: 获取飞鸽用户信息
   * @Param: [secret]
   * @return: com.pkk.alert.common.vo.feige.FeigeUserRespVo
   * @Author: peikunkun
   * @Date: 2019/7/4 0004 下午 4:46
   */
  public static ArrayList<FeigeUserRespVo> buildFeigeUser(String secret) {
    Optional.ofNullable(secret).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽账户密钥信息"));
    final HashMap<String, Object> requestParam = new HashMap<>();
    requestParam.put("secret", secret);

    final String responseStr = HttpUtil.get(FEIGE_GET_USER_URL, requestParam);
    final ArrayList<FeigeUserRespVo> feigeUserRespVos = new ArrayList<>();
    if (StringUtils.isNotBlank(responseStr)) {
      final JSONObject jsonObject = JSONObject.parseObject(responseStr);
      final JSONArray list = jsonObject.getJSONArray("list");
      for (int i = 0; i < list.size(); i++) {
        FeigeUserRespVo feigeUserRespVo = new FeigeUserRespVo();
        BeanUtil.copyProperties(list.get(i), feigeUserRespVo);
        feigeUserRespVos.add(feigeUserRespVo);
      }
    }
    return feigeUserRespVos;
  }


  /**
   * @Description: 单用户发送信息-新版
   * @Param: [secret, uid, templateId, templateContent, skipUrl, param]
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 下午 1:42
   */
  public static boolean buildSendSingleUser(String secret, Integer uid, String templateId, String templateContent,
      String skipUrl, HashMap<String, FeiGeReqDataVo> param) {
    Optional.ofNullable(secret).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽账户密钥信息"));
    Optional.ofNullable(uid).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽发送用户信息"));
    Optional.ofNullable(templateId).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽模板id"));
    Optional.ofNullable(templateContent).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽模板信息"));
    Optional.ofNullable(param).filter(p -> (p.size() > 0))
        .orElseThrow(() -> new IllegalArgumentException("未知的飞鸽账户密钥信息"));

    JSONObject parent = new JSONObject();
    for (String valueStr : param.keySet()) {
      JSONObject value = new JSONObject();
      value.put("value", param.get(valueStr).getValue());
      value.put("color", param.get(valueStr).getColor());
      parent.put(valueStr, value);
    }

    //拼装参数
    final HashMap<String, Object> requestParam = new HashMap<>();
    requestParam.put("secret", secret);
    requestParam.put("uid", uid.toString());
    requestParam.put("template_id", templateId);
    if (StringUtils.isNotBlank(skipUrl)) {
      requestParam.put("url", skipUrl);
    }
    requestParam.put("data", parent.toJSONString());
    final String responseStr = HttpUtil.post(FEIGE_SEND_USER_SINGLE_NEW, requestParam);
    log.info("发送单用户信息:请求参数:{},响应参数:{}", JSONObject.toJSONString(requestParam), responseStr);
    if (StringUtils.isNotBlank(responseStr)) {

      //// 返回数据为json格式
      //{"code":200,"msg":"Request Ok","data":{"count":1}}

      final JSONObject jsonObject = JSONObject.parseObject(responseStr);
      final Integer code = jsonObject.getInteger("code");
      if (200 == code) {
        return true;
      }
    }
    return false;
  }


  /**
   * @Description: 群发用户信息
   * @Param: [secret, appKey, templateId, templateContent, skipUrl, param]
   * @return: boolean
   * @Author: peikunkun
   * @Date: 2019/7/5 0005 下午 3:17
   */
  public static boolean buildSendMoreUser(String secret, String appKey, String templateId, String templateContent,
      String skipUrl, HashMap<String, FeiGeReqDataVo> param) {
    Optional.ofNullable(secret).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽账户密钥信息"));
    Optional.ofNullable(appKey).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽的appkey"));
    Optional.ofNullable(templateId).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽模板id"));
    Optional.ofNullable(templateContent).orElseThrow(() -> new IllegalArgumentException("未知的飞鸽模板信息"));
    Optional.ofNullable(param).filter(p -> (p.size() > 0))
        .orElseThrow(() -> new IllegalArgumentException("未知的飞鸽账户密钥信息"));

    JSONObject parent = new JSONObject();
    for (String valueStr : param.keySet()) {
      JSONObject value = new JSONObject();
      value.put("value", param.get(valueStr).getValue());
      value.put("color", param.get(valueStr).getColor());
      parent.put(valueStr, value);
    }

    //拼装参数
    final HashMap<String, Object> requestParam = new HashMap<>();
    requestParam.put("secret", secret);
    requestParam.put("app_key", appKey);
    requestParam.put("template_id", templateId);
    if (StringUtils.isNotBlank(skipUrl)) {
      requestParam.put("url", skipUrl);
    }
    requestParam.put("data", parent.toJSONString());
    final String responseStr = HttpUtil.post(FEIGE_SEND_USER_MORE_NEW, requestParam);
    log.info("发送群组信息:请求参数:{},响应参数:{}", JSONObject.toJSONString(requestParam), responseStr);
    if (StringUtils.isNotBlank(responseStr)) {
      //code	请求状态码，200为成功，其他为错误
      //msg	状态码对应提示，Ok为成功，其他提示错误消息
      final JSONObject jsonObject = JSONObject.parseObject(responseStr);
      final Integer code = jsonObject.getInteger("code");
      if (200 == code) {
        return true;
      }
    }
    return false;
  }

}
